//
//  UWGamesController.m
//  Urban Words
//
//  Created by Tim Brandin on 2011-11-21.
//  Copyright (c) 2011 Sypreme. All rights reserved.
//

#import "UWGamesController.h"
#import "UILabel+UW.h"
#import "UWTableViewCell.h"

@implementation UWGamesController

@synthesize scrollDelegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedString(@"Games", @"Games");
        self.tabBarItem.image = [UIImage imageNamed:@"games"];
        
        
        UIView *labelContainer = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 210, 30)];
        labelContainer.backgroundColor = [UIColor clearColor];
        //UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 6, 210, 30)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 210, 30)];
        [label setSpecialText:@"Urban Words"];
        [labelContainer addSubview:label];
        self.navigationItem.titleView = labelContainer;
        [label release];
        
        
		self.tableView.delaysContentTouches = NO;
        UIImage *img = [UIImage imageNamed:@"LogoIcon"];
        
        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [infoButton setImage:img forState:UIControlStateNormal];
        
        CGRect newFrame = CGRectMake(0.0, 0.0, img.size.width, img.size.height);
        newFrame.origin.x += 15;
        
        infoButton.frame = newFrame;
        [infoButton addTarget:[[UIApplication sharedApplication] delegate] action:@selector(settings) forControlEvents:UIControlEventTouchUpInside];
        
        [[self navigationItem] setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:infoButton]];
        
        self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"uw-gamesscreen-background"]];
        
        UIBarButtonItem *addGame = [[UIBarButtonItem alloc] initWithTitle:@"New Game" 
                                                                    style:UIBarButtonItemStyleDone 
                                                                    target:self 
                                                                    action:@selector(newGame)];
        
        [self.navigationItem setRightBarButtonItem:addGame];
        
        [self.tableView setSeparatorColor:[UIColor clearColor]];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    gamesManager = [UWGamesManager sharedManager];
    
    [gamesManager setDelegate:self];
    
    friendManager = [UWFriendManager sharedManager];
    
    [friendManager setDelegate:self];

    [gamesManager updateGames];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [gamesManager setDelegate:nil];
    [friendManager unsetDelegate:self];
    [friendManager release];
    [gamesManager release];
    
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[self tableView] reloadData];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

/*
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [gamesManager getSectionAt:section];
}
 */

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [gamesManager ongoing];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
    cell.parent = self;
    [cell setAtIndexPath:indexPath];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135;
}

- (void)newGame 
{

}

- (void)gamesChanged:(BOOL)success 
{
    if (success) {
        [self.tableView reloadData];
        [friendManager updateFriends];
        // tell refreshView to dismiss reloadIcon
        [self stopLoading];
    } else {
        // tell refreshView to dismiss reloadIcon
        [self stopLoadingFailed];
    }
}

- (void)friendsChanged:(BOOL)success 
{
    if (success) {
        [self.tableView reloadData];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.scrollDelegate stopScroll];
    
    [super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}

@end
